package com.wlc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlc.mapper.CmsCategoryMapper;
import com.wlc.service.CmsCategoryService;
import com.wlc.vo.CmsCategory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王立朝
 * @date 2023/3/30
 * @description:
 */
@Service
public class CmsCategoryServiceImpl implements CmsCategoryService {

    @Resource
    private CmsCategoryMapper cmsCategoryMapper;

    @Override
    public List<CmsCategory> findMenu3() {
        QueryWrapper<CmsCategory> cmsCategoryQueryWrapper = new QueryWrapper<>();
        // 查询顶级菜单
        cmsCategoryQueryWrapper.eq("category_id","");
        List<CmsCategory> cmsCategories = cmsCategoryMapper.selectList(cmsCategoryQueryWrapper);
        if (cmsCategories != null && cmsCategories.size() > 0) {
            cmsCategories.forEach(this::findAllChild);
        }
        return cmsCategories;
    }



    public void findAllChild(CmsCategory cmsCategory) {
        QueryWrapper<CmsCategory> cmsCategoryQueryWrapper = new QueryWrapper<>();
        // 查询顶级菜单
        cmsCategoryQueryWrapper.eq("category_id", cmsCategory.getId());
        List<CmsCategory> cmsCategories = cmsCategoryMapper.selectList(cmsCategoryQueryWrapper);
        cmsCategory.setChildMenu(cmsCategories);

        if (cmsCategories != null && cmsCategories.size() > 0) {
            cmsCategories.forEach(this::findAllChild);
        }
    }


    public void findAllChild2(CmsCategory cmsCategory) {
        QueryWrapper<CmsCategory> cmsCategoryQueryWrapper = new QueryWrapper<>();
        // 查询顶级菜单
        cmsCategoryQueryWrapper.eq("leaf","0");
        List<CmsCategory> cmsCategories = cmsCategoryMapper.selectList(cmsCategoryQueryWrapper);
        // 排除掉 顶级菜单
        List<CmsCategory> collect = cmsCategories.stream().filter(e -> !e.getCategoryId().equals("")).collect(Collectors.toList());

        cmsCategory.setChildMenu(collect);
        if (collect != null && collect.size() > 0) {
            collect.forEach(this::findAllChild);
        }


    }

    @Override
    public List<CmsCategory> findMenu2() {
        //数据查询
        List<CmsCategory> list = cmsCategoryMapper.selectList(new QueryWrapper());
        //新建一个用于接收数据的list
        List<CmsCategory> resultList = new ArrayList<>();
        for (CmsCategory result : list) {
            if (result.getCategoryId() == null) {
                resultList.add(getMenuTree(result, list));
            }
            if (result.getLeaf().equals("0")) {
                //调用方法给子类添加数据
                resultList.add(getMenuTree(result, list));
            }
        }
        return resultList;
    }

    private CmsCategory getMenuTree(CmsCategory result, List<CmsCategory> list) {
        for (CmsCategory menu : list) {
            //如果父类主键等于传过来实体类的ID
            if (menu.getCategoryId() == result.getId()) {
                if (result.getChildMenu() == null) {
                    result.setChildMenu(new ArrayList<>());
                }
                // 递归调用
                result.getChildMenu().add(getMenuTree(menu, list));
            }
        }
        return result;
    }
}
